package wjh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import wjh.mapper.UserInfoMapper;
import wjh.pojo.Patient;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class UserInfoImpl implements UserInfoService{
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Patient findUserById(int id) {
        return userInfoMapper.findUserById(id);
    }

    @Override
    public void updateInfo(Patient patient, MultipartFile file, HttpServletRequest request) {
        String photo="a0.jpg";
        if (file.getSize()>0){
            //获取文件的原始名称 a.jog  bbb.gif
            String originalFilename = file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String prefix = UUID.randomUUID().toString().replaceAll("-","");
            photo=prefix+suffix;
            String realPath = request.getServletContext().getRealPath("/static/img");
            //执行上传
            try {
                file.transferTo(new File(realPath,photo));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        patient.setAvatar(photo);
        userInfoMapper.updateInfo(patient);
    }

    @Override
    public Patient findUserLogin(String name, String password) {
        return userInfoMapper.findUserLogin(name,password);
    }

    @Override
    public Patient findUserId(String name, String password) {
        return userInfoMapper.findUserId(name,password);
    }

    @Override
    public Patient findUserRegister(String name) {
        return userInfoMapper.findUserRegister(name);
    }

    @Override
    public void register(String name, String password) {
        userInfoMapper.register(name,password);
    }
}
